using System;
using System.ServiceModel.Syndication;
using System.Web.Mvc;
using Azure.Infrastructure;
using Azure.Tests.Web.Controllers;
using Rhino.Mocks;
using Xunit;

namespace Azure.Tests.Infrastructure
{
    public class FeedResultFacts : ControllerFacts<BaseControllerFacts.TestableController>
    {
        private readonly SyndicationFeed _feed = MockRepository.GenerateStub<SyndicationFeed>();
        private readonly ControllerContext _controllerContext;

        public FeedResultFacts()
        {
            _controllerContext = ClassUnderTest.ControllerContext;

        }

        [Fact]
        public void ContentTypeIsOnExecuteResult()
        {
            var result = new TestFeedResult(_feed);

            result.ExecuteResult(_controllerContext);

            Assert.Equal(TestFeedResult.TestContentType, ClassUnderTest.Response.ContentType);
        }

        [Fact]
        public void ResponseStreamIsWrittenTo()
        {
            var result = new TestFeedResult(_feed);

            result.ExecuteResult(_controllerContext);

            Assert.Contains("http://www.w3.org/2005/Atom", ResponseString.ToString());
        }

        class TestFeedResult : FeedResult
        {
            public const string TestContentType = "application/test";

            public TestFeedResult(SyndicationFeed feed) : base(feed)
            {
            }

            protected override SyndicationFeedFormatter CreateFormatter(SyndicationFeed feed)
            {
                return new Rss20FeedFormatter(feed);
            }

            protected override string ContentType
            {
                get { return TestContentType; }
            }
        }
    }
}